package com.hemi.rhet;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TabWidget;
import android.widget.TextView;

import com.hemi.ui.tab.TabHostActivity;
import com.hemi.ui.tab.TabItem;

/**
 * <p>整个流程就像使用ListView自定BaseAdapter一样</p>
 * 
 * <p>如果要自定义TabHostActivity的Theme，并且不想要头部阴影
 * 一定要添加这个android:windowContentOverlay = null</p>
 * 
 * <p>如果想在别的项目里面使用TabHostActivity
 * 可以项目的属性里面找到Android，然后在Library部分添加这个项目(Api)
 * <a href="http://www.cnblogs.com/qianxudetianxia/archive/2011/05/01/2030232.html">如何添加</a></p>
 * */
public class MainTabActivity extends TabHostActivity {

	
	List<TabItem> mItems;
	private LayoutInflater mLayoutInflater;
	
	/**在初始化TabWidget前调用
	 * 和TabWidget有关的必须在这里初始化*/
	@Override
	protected void prepare() {
		Intent homeIntent=new Intent(this, TopicsListActivity.class);
		TabItem home = new TabItem("TAG_HOME",
				"首页",									// title
				R.layout.tab_home_top,					//top layout
				R.drawable.icon_home_nor,					// icon
				R.drawable.tab_item_background,			// background
				homeIntent);	// intent
		TabItem msg = new TabItem(
				"TAG_MESSAGE",
				"信息",
				R.layout.tab_message_top,
				R.drawable.icon_meassage_nor,
				R.drawable.tab_item_background,
				new Intent(this, ActivityUserTopics.class));
		
		TabItem info = new TabItem(
				"TAG_PROFILE",
				"资料",
				R.layout.tab_profile_top,
				R.drawable.icon_selfinfo,
				R.drawable.tab_item_background,
				new Intent(this, ActivityUserProfile.class));
		
		
		TabItem square = new TabItem(
				"TAG_SQUARE",
				"广场",
				R.layout.tab_square_top,
				R.drawable.icon_square,
				R.drawable.tab_item_background,
				new Intent(this, WebViewDemo.class));
		
		TabItem more = new TabItem(
				"TAG_MORE",
				"更多",
				R.layout.tab_more_top,
				R.drawable.icon_more,
				R.drawable.tab_item_background,
				new Intent(this, ActivitySettings.class));
		
		mItems = new ArrayList<TabItem>();
		mItems.add(home);
		mItems.add(info);
		mItems.add(msg);
		mItems.add(square);
		mItems.add(more);

		// 设置分割线
		TabWidget tabWidget = getTabWidget();
		tabWidget.setDividerDrawable(R.drawable.tab_divider);
		
		mLayoutInflater = getLayoutInflater();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setCurrentTab(0);
		ImageView image  =  (ImageView)findViewById(R.id.new_topic_image);
		
		image.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				Intent intent= new Intent(MainTabActivity.this, ActivityTopicIssure.class);
				startActivity(intent);
			}
		});
		
		
		ImageView  reload_image_view = (ImageView) findViewById(R.id.reload_image_view);

	reload_image_view.setOnClickListener(new OnClickListener(){
		public void onClick(View arg0) {
			System.out.println("freshListener!=null" +(freshListener!=null));
			if(freshListener!=null)
			freshListener.fresh();
		}
	});
	}
	
	
	RefreshListener freshListener;
	public void setOnfreshListener(RefreshListener freshListener){
		this.freshListener =freshListener;
	}
	
	
	/**tab的title，icon，边距设定等等*/
	@Override
	protected void setTabItemTextView(TextView textView, int position) {
		textView.setPadding(3, 3, 3, 3);
		textView.setText(mItems.get(position).getTitle());
		textView.setBackgroundResource(mItems.get(position).getBg());
		textView.setCompoundDrawablesWithIntrinsicBounds(0, mItems.get(position).getIcon(), 0, 0);
		
	}
	
	/**tab唯一的id*/
	@Override
	protected String getTabItemId(int position) {
		return mItems.get(position).getId();	// 我们使用title来作为id，你也可以自定
	}

	/**点击tab时触发的事件*/
	@Override
	protected Intent getTabItemIntent(int position) {
		return mItems.get(position).getIntent();
	}

	@Override
	protected int getTabItemCount() {
		return mItems.size();
	}
	
	 
	/**自定义头部文件*/
	protected View getTop(String tabId) {
		View v = null;
		if(tabId!=null)
		{
			for(TabItem itm:mItems)
			{
				if(itm.getId().equals(tabId)){
					  v =  mLayoutInflater.inflate(itm.getTopLayout(), null);
				}
			}
		}
		if(v==null)
		  v=mLayoutInflater.inflate(R.layout.tab_home_top, null);
		return v;
	}
	
	  protected void onChanged(String tabId){
		  if(tabId.equals("TAG_HOME"))
		  {
		  ImageView image  =  (ImageView)findViewById(R.id.new_topic_image);
			  image.setOnClickListener(new OnClickListener(){
					public void onClick(View arg0) {
						Intent intent= new Intent(MainTabActivity.this, ActivityTopicIssure.class);
						startActivityForResult(intent, R.id.new_topic_image);
					}
				});
		  }
	  }
	interface RefreshListener {
		public void fresh();
	}
	
	
}
